package com.foreverwin.jzymes.websocket;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.foreverwin.jzymes.common.service.IActivityRuleAdviceService;
import com.foreverwin.jzymes.common.service.ICncReqLogAdviceService;
import com.foreverwin.jzymes.common.service.IProductionShiftService;
import com.foreverwin.jzymes.common.service.IResourceAdviceService;
import com.foreverwin.jzymes.framework.annotation.WebSocketHandler;
import com.foreverwin.jzymes.framework.event.AppEvent;
import com.foreverwin.jzymes.framework.util.DateUtil;
import com.foreverwin.jzymes.framework.websocket.TextMessageWebSocketWraper;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;

@WebSocketHandler(channel = "Z_DB_SUB_LINE")
public class SubLineHandler extends TextMessageWebSocketWraper {

    @Autowired
    IActivityRuleAdviceService activityRuleAdviceService;
    @Autowired
    WebSocketService webSocketService;
    @Autowired
    IProductionShiftService productionShiftService;
    @Autowired
    IResourceAdviceService resourceAdviceService;
    @Autowired
    ICncReqLogAdviceService cncReqLogAdviceService;

    private Map<String,String> activityRuleMap;

    public SubLineHandler(){
        events = new ArrayList();
        events.add( "RESOURCE_STATUS_CHANGE" );
        events.add( "CNC_CARRIER_OUT" );
        events.add( "AGV_DISPATCH" );
        events.add( "TRANSHIP_CURRENT_QTY" );
    }

    @Override
    public void doAfterOpen(Connection connection) {
        activityRuleMap = activityRuleAdviceService.findActivityRules( getChannel() );
    }

    @Override
    public void doAfterClose() {

    }

    @Override
    public void onAppEvent(AppEvent appEvent) {
        String event = appEvent.getName();
        JSONObject eventData = (JSONObject) appEvent.getData();

        if( "RESOURCE_STATUS_CHANGE".equals( event ) ){
            String site = eventData.getString( "SITE" );
            String resrce = eventData.getString( "RESRCE" );
            String status = eventData.getString( "STATUS" );

            JSONObject response = new JSONObject();
            response.put( "id", resrce );
            response.put( "status", status );
            sendMessage( "WS_RESOURCE_STATUS", response );
        }

        if( "CNC_CARRIER_OUT".equals( event ) ){
            String site = eventData.getString( "SITE" );
            String resrce = eventData.getString( "RESRCE" );

            /**发送cnc产出本体数据到页面*/
            Date[] dateRange = productionShiftService.findShiftRange( site, new Date() );
            Map<String,Object> countYield = cncReqLogAdviceService.countCncYield( site, resrce, dateRange[0], dateRange[1] );
            JSONObject cncObj = new JSONObject();
            cncObj.put( "id", countYield.get( "RESRCE" ) );
            cncObj.put( "qty", countYield.get( "QTY" ) );
            JSONArray cncs = new JSONArray();
            cncs.add( cncObj );
            JSONObject responseBody = new JSONObject();
            responseBody.put( "cncs", cncs );
            sendMessage( "WS_BRANCH_CNC_INFRA", responseBody );
        }

        if( "AGV_DISPATCH".equals( event ) ){
            String channel = eventData.getString( "CHANNEL" );
            if( channel.equals( getChannel() ) ){
                String messageType = eventData.getString( "MESSAGE_TYPE" );
                JSONObject value = eventData.getJSONObject( "VALUE" );
                sendMessage( messageType, value );
            }
        }

        if( "TRANSHIP_CURRENT_QTY".equals( event ) ){
            String channel = eventData.getString( "CHANNEL" );
            if( channel.equals( getChannel() ) ){
                String messageType = eventData.getString( "MESSAGE_TYPE" );
                JSONObject value = eventData.getJSONObject( "VALUE" );
                sendMessage( messageType, value );
            }
        }

    }

    public JSONObject doReceiveAndReply( String messageType, JSONObject requestParams ){

        JSONObject responseBody = null;
        String channel = getChannel();

        if( "WS_INIT_DATA".equals( messageType ) ){
            String site = activityRuleMap.get( "SITE" );
            String workCenter = activityRuleMap.get( "WORK_CENTER" );
            responseBody = findBranchCncInfo( site, workCenter );
            JSONObject transhipData = webSocketService.findPageData( site, channel, "TRASHIP_INIT_DATA" );
            if( transhipData != null ){
                responseBody.put( "ports", transhipData.getJSONArray( "ports" ) );
            }
            JSONObject agvData = webSocketService.findPageData( site, channel, "AGV_INIT_DATA" );
            if( agvData != null ){
                responseBody.put( "agvs", agvData.getJSONArray( "agvs" ) );
            }

        }

        return responseBody;
    }


    public JSONObject findBranchCncInfo( String site, String workCenter ){

        List<String> resourceList = resourceAdviceService.findResourceListByWorkCenter( site, workCenter );

        List<Map<String,Object>> cncs = new ArrayList<>();

        Date[] dateRange = productionShiftService.findShiftRange( site, new Date() );
        List<Map<String,Object>> cncYieldList = cncReqLogAdviceService.countCncYieldByDateRange( site, dateRange[0], dateRange[1] );
        for( Map<String,Object> cncYeildMap : cncYieldList ){
            String resource = (String) cncYeildMap.get( "RESRCE" );
            if( resourceList.contains( resource ) ){
                Map<String,Object> cncMap = new HashMap<>();
                cncMap.put( "id", resource );
                cncMap.put( "qty", cncYeildMap.get( "QTY" ) );
                cncMap.put( "status", resourceAdviceService.findResourceStatus( site, resource ) );
                cncs.add( cncMap );
                resourceList.remove( resource );
            }
        }

        for( String resource : resourceList ){
            Map<String,Object> cncMap = new HashMap<>();
            cncMap.put( "id", resource );
            cncMap.put( "qty", 0 );
            cncMap.put( "status", resourceAdviceService.findResourceStatus( site, resource ) );
            cncs.add( cncMap );
        }

        JSONObject responseBody = new JSONObject();
        responseBody.put( "time", DateUtil.format( new Date(), "yyyy-MM-dd HH:mm:ss" ) );
        responseBody.put( "cncs", cncs );

        return responseBody;
    }
}
